﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YmtAuth.AppService;
using YmtSystem.CrossCutting;

namespace YmtUserAuth.DataImport2
{
    public class ReadUserMobile
    {

        public static void WriteFile()
        {
            var filePath = System.Configuration.ConfigurationManager.AppSettings["data2"];
            var fullPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data" + "\\" + filePath);
            Console.WriteLine("执行导入数据");
            var watch = Stopwatch.StartNew();
            var fields = ReadFile.SyncRead(fullPath);
            //var listuserId = new List<int>();
            //var listMobile = new List<string>();
            var list = new List<Temp>();
            foreach (var item in fields)
            {
                var userId = Convert.ToInt32(item[0]);
                var moible = item[1];
                var t = new Temp
                {
                    UserId = userId,
                    Mobile = moible
                };
                list.Add(t);
                //listuserId.Add(userId);
                //listMobile.Add(moible);
            }
            var order = list.OrderBy(e => e.UserId);
            var logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\mobile_orderby_811.log");

            order.Each(e =>
            {
                System.IO.File.AppendAllText(logPath, string.Format("{0},{1}\r\n", e.UserId, e.Mobile));
            });
            Console.WriteLine("ok");
            Console.Read();
        }

        public static Tuple<List<int>, List<string>> UserMobileFilter()
        {
            var filePath = System.Configuration.ConfigurationManager.AppSettings["data2"];
            var fullPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data" + "\\" + filePath);
            Console.WriteLine("执行导入数据");
            var watch = Stopwatch.StartNew();
            var fields = ReadFile.SyncRead(fullPath);
            var listuserId = new List<int>();
            var listMobile = new List<string>();
            foreach (var item in fields)
            {
                var userId = Convert.ToInt32(item[0]);
                var moible = item[1];

                listuserId.Add(userId);
                listMobile.Add(moible);
            }

            return Tuple.Create<List<int>, List<string>>(listuserId, listMobile);
        }
        public static List<int> UserIdFilter()
        {
            var filePath = System.Configuration.ConfigurationManager.AppSettings["data2"];
            var fullPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data" + "\\" + filePath);
            Console.WriteLine("执行导入数据");
            var watch = Stopwatch.StartNew();
            var fields = ReadFile.SyncRead(fullPath);
            var listuserId = new List<int>();

            foreach (var item in fields)
            {
                var userId = Convert.ToInt32(item[0]);


                listuserId.Add(userId);

            }

            return listuserId;
        }
        public static void FindAllUser()
        {
            try
            {
                var userIds = UserIdFilter();
                Console.WriteLine("开始执行");
                YmatouFramework.Start();

                ImportUser.ReadUsers(userIds);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.WriteLine("ok");
            Console.Read();
        }
        public static void FindAllMobile()
        {
            try
            {
                var mobileList = UserMobileFilter().Item2;
                Console.WriteLine("开始执行");
                YmatouFramework.Start();
                foreach (var item in mobileList)
                {

                }
                ImportUser.ReadMobile(mobileList);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.WriteLine("ok");
            Console.Read();
        }


        public static void FindALlUserBills()
        {
            var db = System.Configuration.ConfigurationManager.AppSettings["db"] ?? "Ymt_Auth_201406";
            var tb = System.Configuration.ConfigurationManager.AppSettings["tb"] ?? "ULC_201406";
            var strPageCount = System.Configuration.ConfigurationManager.AppSettings["pageCount"] ?? "0";
            var pageCount = Convert.ToInt32(strPageCount);
            var strPageSize = System.Configuration.ConfigurationManager.AppSettings["pageSize"] ?? "0";
            var pageSize = Convert.ToInt32(strPageSize);

            YmatouFramework.Start();
            ImportUser.ReadAllUsers(db, tb, pageSize, pageCount);
        }

        public static void AnalysisUserBills()
        {
            var watch = Stopwatch.StartNew();
            var logPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\alluser-error.log");
            var logUserIdPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\alluser-Userid-group.log");
            var loginIdPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\alluser-loginId-group.log");
            var loginEmailPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\alluser-email-group.log");
            var loginMobilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "log\\alluser-mobile-group.log");

            var fullPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data" + "\\" + "alluser.csv");
            var line = ReadFile.SyncRead(fullPath);
            var list = new List<UserTemp>();
            line.Each(l =>
            {
                var ut = new UserTemp();
                ut.UserId = Convert.ToInt32(l[0]);
                ut.LoginId = l[1];
                ut.Email = string.IsNullOrEmpty(l[2]) ? null : l[2];
                ut.Mobile = l.Length == 4 ? l[3] : null;

                var _tmpLogin = ut.LoginId.ToLower();
                if (_tmpLogin != ut.LoginId)
                {
                    var str = string.Format("{0},{1},{2},{3}\r\n", ut.UserId, ut.LoginId, ut.Email, ut.Mobile);
                    System.IO.File.AppendAllText(logPath, str);
                }
                list.Add(ut);
            });
            Console.WriteLine("数据填充完成，开始分析 ,共 {0}", list.Count);

            var userIdLookUp = list.GroupBy(e => e.UserId).Where(e => e.Count() > 1);
            Console.WriteLine("Uid 分组统计大于1 的有 ：{0}", userIdLookUp.Count());
            userIdLookUp.Each(e =>
            {
                e.Select(_e => _e).Each(u =>
                {
                    var str = string.Format("{0},{1},{2},{3}\r\n", u.UserId, u.LoginId, u.Email, u.Mobile);
                    System.IO.File.AppendAllText(logUserIdPath, str);
                });
            });


            var userLoginIdLookUp = list.GroupBy(e => e.LoginId).Where(e => e.Count() > 1);
            Console.WriteLine("loginId 分组统计大于1 的有 ：{0}", userLoginIdLookUp.Count());
            userLoginIdLookUp.Each(e =>
            {
                e.Select(_e => _e).Each(u =>
                {
                    var str = string.Format("{0},{1},{2},{3}\r\n", u.UserId, u.LoginId, u.Email, u.Mobile);
                    System.IO.File.AppendAllText(loginIdPath, str);
                });
            });


            var userEmailLookUp = list.Where(e => !string.IsNullOrEmpty(e.Email)).GroupBy(e => e.Email).Where(e => e.Count() > 1);
            Console.WriteLine("emial 分组统计大于1 的有 ：{0}", userEmailLookUp.Count());
            userEmailLookUp.Each(e =>
            {
                e.Select(_e => _e).Each(u =>
                {
                    var str = string.Format("{0},{1},{2},{3}\r\n", u.UserId, u.LoginId, u.Email, u.Mobile);
                    System.IO.File.AppendAllText(loginEmailPath, str);
                });
            });


            var userMobileLookUp = list.Where(e => !string.IsNullOrEmpty(e.Mobile)).GroupBy(e => e.Mobile).Where(e => e.Count() > 1);
            Console.WriteLine("mobile 分组统计大于1 的有 ：{0}", userMobileLookUp.Count());
            userMobileLookUp.Each(e =>
            {
                e.Select(_e => _e).Each(u =>
                {
                    var str = string.Format("{0},{1},{2},{3}\r\n", u.UserId, u.LoginId, u.Email, u.Mobile);
                    System.IO.File.AppendAllText(loginMobilePath, str);
                });
            });

            Console.WriteLine("分析完成 {0}", watch.Elapsed);
            Console.Read();
        }

        public static void ReadUsersLoginHistory() 
        {
            YmatouFramework.Start();
            ImportUser.ReadUsersLoginHistory(null, null, 1, 1);

        }
        public static void ImportUserRegisterHistory() 
        {
            YmatouFramework.Start();
            //ImportUser.ImportUserRegisterHistory("Ymt_LoginHistory_201402", "UserReg_201409");
            ImportUser.ImportUserloginHistory("Ymt_LoginHistory_201402", "LoginHistory_201408");
            ImportUser.ImportUserloginHistory("Ymt_LoginHistory_201402", "LoginHistory_201409");
            Console.WriteLine("end");
            Console.Read();
        }
        public static void ImportUserloginHistory() {
        
        }
    }

    public class Temp
    {
        public int UserId { get; set; }
        public string Mobile { get; set; }
    }

    public class UserTemp
    {
        public int UserId { get; set; }
        public string LoginId { get; set; }
        public string Mobile { get; set; }
        public string Email { get; set; }
    }
}
